﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Web.Framework;
using System.Data.SqlClient;
using System.Data;
using Job.Data.Interfaces;
using System.Collections;
using Job.Data;
using Job.Entity;
using System.Net.Mail;

namespace Job.Logic.Common
{
    public class CommonLogic
    {
        private readonly ICommon commonDao = DataAccess.commonDao;

        public bool Login(string userName, string password)
        {
            return commonDao.Login(userName, password);
        }

        public DataSet GetPageDataBySql(string sql, int pageSize, int pageIndex, int countType, string sortField, SortDirection sortDire, ArrayList queryParam)
        {
            string strOrder1 = " order by " + sortField;
            string strOrder2 = " order by " + sortField;
            strOrder1 += (sortDire == SortDirection.Descending) ? " DESC" : " ASC";
            strOrder2 += (sortDire == SortDirection.Descending) ? " ASC" : " DESC";
            if (queryParam != null)
            {
                for (int i = 0; i < queryParam.Count; i++)
                {
                    string fieldName = ((SqlParameter)queryParam[i]).ParameterName;
                    string fieldValue = ((SqlParameter)queryParam[i]).Value.ToString();
                    sql = sql.Replace("@" + fieldName, fieldValue);
                }
            }

            IDataParameter[] arrPara = new IDataParameter[6];
            arrPara[0] = new SqlParameter("@query_str", DbType.String);
            arrPara[0].Value = sql;

            arrPara[1] = new SqlParameter("@order_str1", DbType.String);
            arrPara[1].Value = strOrder1;

            arrPara[2] = new SqlParameter("@order_str2", DbType.String);
            arrPara[2].Value = strOrder2;

            arrPara[3] = new SqlParameter("@page_size", DbType.Int32);
            arrPara[3].Value = pageSize;

            arrPara[4] = new SqlParameter("@page_index", DbType.Int32);
            arrPara[4].Value = pageIndex;

            arrPara[5] = new SqlParameter("@count_type", DbType.Int32);
            arrPara[5].Value = countType;

            DataSet ds = commonDao.GetPageData(arrPara);//sql, strOrder1, strOrder2, pageSize, pageIndex, countType);
            return ds;
        }

        public DataSet GetPageDataBySqlTwoFieldOrder(string sql, int pageSize, int pageIndex, int countType, string sortField, SortDirection sortDire, ArrayList queryParam)
        {
            string strOrder1 = " order by " + sortField;
            string strOrder2 = " order by " + sortField;
            //strOrder1 += (sortDire == SortDirection.Descending) ? " DESC" : " ASC";
            //strOrder2 += (sortDire == SortDirection.Descending) ? " ASC" : " DESC";
            strOrder1 = string.Format(strOrder1,(sortDire == SortDirection.Descending)?" DESC":" ASC",(sortDire == SortDirection.Descending)?" DESC":" ASC");
            strOrder2 = string.Format(strOrder2,(sortDire == SortDirection.Descending)?" ASC":" DESC",(sortDire == SortDirection.Descending)?" ASC":" DESC");

            if (queryParam != null)
            {
                for (int i = 0; i < queryParam.Count; i++)
                {
                    string fieldName = ((SqlParameter)queryParam[i]).ParameterName;
                    string fieldValue = ((SqlParameter)queryParam[i]).Value.ToString();
                    sql = sql.Replace("@" + fieldName, fieldValue);
                }
            }

            IDataParameter[] arrPara = new IDataParameter[6];
            arrPara[0] = new SqlParameter("@query_str", DbType.String);
            arrPara[0].Value = sql;

            arrPara[1] = new SqlParameter("@order_str1", DbType.String);
            arrPara[1].Value = strOrder1;

            arrPara[2] = new SqlParameter("@order_str2", DbType.String);
            arrPara[2].Value = strOrder2;

            arrPara[3] = new SqlParameter("@page_size", DbType.Int32);
            arrPara[3].Value = pageSize;

            arrPara[4] = new SqlParameter("@page_index", DbType.Int32);
            arrPara[4].Value = pageIndex;

            arrPara[5] = new SqlParameter("@count_type", DbType.Int32);
            arrPara[5].Value = countType;

            DataSet ds = commonDao.GetPageData(arrPara);//sql, strOrder1, strOrder2, pageSize, pageIndex, countType);
            return ds;
        }


        public int GetCountBySql(string sql)
        {
            int Default_Page_Size = 10;
            int Default_Current_Page = 1;
            int Default_Count_Type = 1;
            DataSet ds = GetPageDataBySql(sql, Default_Page_Size, Default_Current_Page, Default_Count_Type,
                "", SortDirection.Descending, null);
            int count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            
            return count;
        }

        public int GetCountBySqlTwoFieldOrder(string sql)
        {
            int Default_Page_Size = 10;
            int Default_Current_Page = 1;
            int Default_Count_Type = 1;
            DataSet ds = GetPageDataBySqlTwoFieldOrder(sql, Default_Page_Size, Default_Current_Page, Default_Count_Type,
                "", SortDirection.Descending, null);
            int count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            return count;
        }

        public DataSet FetchLevelDictData(string dictClass)

        {
            

            IDataParameter[] arrPara = new IDataParameter[1];
            arrPara[0] = new SqlParameter("@dict_class", DbType.String);
            arrPara[0].Value = dictClass;

            DataSet ds = commonDao.FetchLevelDictdata(arrPara);//sql, strOrder1, strOrder2, pageSize, pageIndex, countType);
            return ds;
        }

        public DateTime GetServerTime()
        {
            return Convert.ToDateTime( commonDao.GetServerTime());
        }

        private static string MailHost = System.Configuration.ConfigurationSettings.AppSettings["MailHost"];
        private static string MailAccount = System.Configuration.ConfigurationSettings.AppSettings["MailAccount"];
        private static string MailPassword = System.Configuration.ConfigurationSettings.AppSettings["MailPassword"];
        private static string MailFrom = System.Configuration.ConfigurationSettings.AppSettings["MailFrom"];

        public static void SendMail(MailObject mail)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式

            _smtpClient.Host = MailHost; ;//指定SMTP服务器
            _smtpClient.Credentials = new System.Net.NetworkCredential(MailAccount, MailPassword);//用户名和密码

            MailMessage _mailMessage = new MailMessage(MailFrom, mail.MailTo);
            _mailMessage.Subject = mail.Subject;//主题
            _mailMessage.Body = mail.MailContent;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
            _mailMessage.Priority = MailPriority.High;//优先级

            try
            {
                _smtpClient.Send(_mailMessage);

            }
            catch(Exception ex)
            {
                Logger.WriteLog("发送邮件失败:\r\n"+ex.Message);    
            }

        }
    }


}
